package com.example.a2209_mianshi.ui.mall.demo

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.a2209_mianshi.R
import com.example.a2209_mianshi.ui.mall.model.MallBanner
import com.example.a2209_mianshi.ui.mall.model.MallGoods
import com.youth.banner.Banner
import com.youth.banner.loader.ImageLoader

class HomeAdapter(private val items: List<HomeItem>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        private const val VIEW_TYPE_BANNER = 0
        private const val VIEW_TYPE_GOODS = 1
    }

    override fun getItemViewType(position: Int): Int {
        return when (items[position]) {
            is HomeItem.BannerItem -> VIEW_TYPE_BANNER
            is HomeItem.GoodsItem -> VIEW_TYPE_GOODS
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            VIEW_TYPE_BANNER -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_banner, parent, false)
                BannerViewHolder(view)
            }
            VIEW_TYPE_GOODS -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_goods, parent, false)
                GoodsViewHolder(view)
            }
            else -> throw IllegalArgumentException("Unknown view type")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is BannerViewHolder -> {
                val bannerItem = items[position] as HomeItem.BannerItem
                holder.bind(bannerItem.banners)
            }
            is GoodsViewHolder -> {
                val goodsItem = items[position] as HomeItem.GoodsItem
                holder.bind(goodsItem.goods)
            }
        }
    }

    override fun getItemCount(): Int = items.size

    // ViewHolder 实现
    class BannerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val bannerView: Banner = itemView.findViewById(R.id.banner)
        fun bind(banners: List<MallBanner>) {
            var list= mutableListOf<String>()
//            bannerView.setBanners(banners) // 假设 Banner 是一个自定义轮播图控件
            banners.forEach {
                list.add(it.imageUrl)
            }
            bannerView.setImages(list)
            bannerView.setImageLoader(object : ImageLoader(){
                override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                    if (imageView != null) {
                        if (context != null) {
                            Glide.with(context).load(path).into(imageView)
                        }
                    }
                }
            })
            bannerView.start()
        }
    }

    class GoodsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val goodsImage: ImageView = itemView.findViewById(R.id.goods_image)
        private val goodsName: TextView = itemView.findViewById(R.id.goods_name)
        fun bind(goods: List<MallGoods>) {
            goods.forEach {
                Glide.with(itemView.context).load(it.imageUrl).into(goodsImage)
                goodsName.text = it.name
            }
        }
    }
}